package com.yft.dao.competence;

import com.yft.dao.base.BaseConstant;
import com.yft.dao.base.BaseDao;
import com.yft.entity.base.BaseEntityUtils;
import com.yft.entity.base.Order;
import com.yft.entity.base.Pager;
import com.yft.entity.competence.SysCompetence;
import org.hibernate.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * 
 * @author zhangming
 *
 */
@Service("sysCompetenceDao")
public class SysCompetenceDao {

	@Autowired(required = true)
	@Qualifier(BaseConstant.DAO_NAME)
	private BaseDao dao;

	// ---------------------------------------------------------
	
	public Object addSysCompetence(SysCompetence sysCompetence) {
		return dao.addEntity(sysCompetence);
	}
	
	public boolean saveSysCompetence(List<SysCompetence> sysCompetence){
		return dao.saveAllEntity(sysCompetence);
	}
	
	public boolean saveSysCompetence(SysCompetence sysCompetence){
		return dao.saveOrUpdateEntity(sysCompetence);
	}
	
	public boolean deleteSysCompetence(Serializable sysCompetencePK){
		return dao.deleteEntity(SysCompetence.class,sysCompetencePK);
	}

	public boolean deleteAllSysCompetence(String[] sysCompetencePK){
		return dao.deleteAllEntity(SysCompetence.class,sysCompetencePK);
	}
	
	public boolean updateSysCompetence(SysCompetence sysCompetence) {
		return dao.updateEntity(sysCompetence, sysCompetence.getSysCompetenceId());
	}
	
	public boolean modifySysCompetence(SysCompetence sysCompetence) {
		return dao.updateEntity(sysCompetence);
	}
	
	public SysCompetence getSysCompetence(Object sysCompetencePK){
		return (SysCompetence)dao.getEntity(SysCompetence.class,(Serializable)sysCompetencePK);
	}

	@SuppressWarnings("unchecked")
	public List<Object[]> getSysCompetenceList(String[] selectProperty,SysCompetence condition,Order order,Pager pager){
		return getList(dao.parseSelectProperty(selectProperty),condition,order,pager);
	}
	
	@SuppressWarnings("unchecked")
	public List<SysCompetence> getSysCompetenceList(SysCompetence condition,Order order,Pager pager){
		return getList("from SysCompetence c where 1=1 ",condition,order,pager);
	}
	
	public boolean updateValues(SysCompetence condition){
		Integer primaryKey = condition.getSysCompetenceId();
		if(primaryKey==null) return false;
		Query query=dao.getHibernateTemplate().getSessionFactory().getCurrentSession().createQuery(makeValues(condition,primaryKey));
		query.setProperties(condition);
		Integer result = query.executeUpdate();
		return result!=null && result.intValue()==1 ? true : false;
	}
	
	// --------------------------------------------------------------------------------  private methods
	@SuppressWarnings("unchecked")
	private List getList(String q,SysCompetence condition,Order order,Pager pager){
		if(q==null || q.trim().length()==0) q = "from SysCompetence " + BaseDao.entityKey + " where 1=1 ";
		q = makeCondition(q,condition); 
		q += order!=null? order.toString():"";
		return dao.getListPager(q,condition,pager);
	}
	
	private String makeCondition(String q,SysCompetence condition){
		if(condition==null) condition = new SysCompetence();
		
		q += BaseEntityUtils.makeBaseCondition(condition);
		
		return q;
	}
	
	private String makeValues(SysCompetence con,Integer primaryKey){
		if(con==null) con = new SysCompetence();
		String q = "update SysCompetence set updateTime=current_timestamp()";
		
		q += " where sysCompetenceId='"+primaryKey+"'";
		return q;
	}
}
